﻿using CoreEx.Common.Ioc;
using CoreEx.Common.Proxy;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;
using LinFu.Proxy;
using LinFu.Proxy.Interfaces;

namespace CoreEx.Common.Implementation.Proxy
{
    [PostProcessor]
    public class ProxyMethodBuilderPostProcessor : PostProcessorBase<IMethodBuilder>
    {
        public override void Process(IServiceRequestResult result)
        {
            var proxyFactory = new ProxyFactory();

            var proxyMethodBuilder = (IMethodBuilder)result.OriginalResult;

            var proxyMethodBuilderInterceptor =
                result.Container.GetService<IInvokeWrapper>("ProxyMethodBuilderInterceptor", proxyMethodBuilder);

            var proxyMethodBuilderProxy =
                proxyFactory.CreateProxy<IMethodBuilder>(proxyMethodBuilderInterceptor, typeof(IActualTarget));

            result.ActualResult = proxyMethodBuilderProxy;
        }
    }
}
